<?php
namespace App\Api\Controllers\Huodong;


use App\Api\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\AlipayHongba;
use App\Models\Store;
use Illuminate\Http\Request;

class AlipayHongbao extends BaseController
{

    public function add(Request $request)
    {
        try {
            $public = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $store_name = $request->get('store_name', '');
            $content = $request->get('content', '');
            $remark = $request->get('remark', '');

            if ($public->type == "merchant") {
                $user_id = $public->merchant_id;
                $user_name = $public->name;

            }

            if ($public->type == "user") {
                $user_id = $public->user_id;
                $user_name = $public->name;
            }

            $check_data = [
                'content' => '红包内容'
            ];

            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $data = [
                'store_id' => $store_id,
                'store_name' => $store_name,
                'create_user_id' => $user_id,
                'create_user_name' => $user_name,
                'content' => $content,
                'remark' => $remark,
                'model_type' => $public->type
            ];
            $obj = AlipayHongba::create($data);


            $this->status = 1;
            $this->message = '添加成功';
            return $this->format($data);


        } catch (\Exception $exception) {
            return json_encode(['status' => -1, 'message' => $exception->getMessage()]);
        }


    }


    public function del(Request $request)
    {
        try {
            $public = $this->parseToken();
            $id = $request->get('id', '');

            $check_data = [
                'id' => '红包id'
            ];

            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }


            $where[] = ['model_type', '=', $public->type];

            if ($id) {
                $where[] = ['id', '=', $id];
            }

            $obj = AlipayHongba::where($where)->delete();

            $data = [
                'id' => $id
            ];

            $this->status = 1;
            $this->message = '删除成功';
            return $this->format($data);


        } catch (\Exception $exception) {
            return json_encode(['status' => -1, 'message' => $exception->getMessage()]);
        }


    }


    public function get_list(Request $request)
    {
        try {
            $public = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $content = $request->get('content', '');
            $where = [];

            if ($public->type == "merchant") {
                $user_id = $public->merchant_id;
            }

            if ($public->type == "user") {
                $user_id = $public->user_id;
            }

            $create_user_id = $request->get('create_user_id', $user_id);


            $where[] = ['model_type', '=', $public->type];

            if ($store_id) {
                $where[] = ['store_id', '=', $store_id];

            }

            if ($create_user_id) {
                $where[] = ['create_user_id', '=', $create_user_id];
            }


            if ($content) {
                $where[] = ['content', '=', $content];
            }


            $obj = AlipayHongba::where($where);
            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);


        } catch (\Exception $exception) {
            return json_encode(['status' => -1, 'message' => $exception->getMessage()]);
        }


    }
    
    //公钥加密
    public function PublicEncrypt(){
        $data=array(
            'qr_code'=>'135180663496762450',
            'mer_id'=>'123456789012345',
            'term_id'=>'123456',
            'out_trade_no'=>'wxWft20210423102014480806145',
            'order_amt'=>'1',
            'trade_date'=>'20210423',
            'trade_time'=>'102014',
        );
        $signPars=json_encode($data);
        $public_key='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDXOrhOKXoCu5sNmDDeni830Ppfib6/MjW6aZxJQl0nk6/+/pJmtfDvOilBOu8482JYhJIr6kpKpid9fKy8Xa/Uzu8fd5K9/dyAmh2volKutzQ6FemUMvPfJIIDrkXFQ5BoudDIMnIsaURTo7Up/oP2NsDrgk62Q6yS4S623cSy4wIDAQAB';
        $public_rsa_key = "-----BEGIN PUBLIC KEY-----\n" .
            wordwrap($public_key, 64, "\n", true) .
            "\n-----END PUBLIC KEY-----";
        $result='';
        foreach (str_split($signPars, 117) as $chunk) {
            openssl_public_encrypt($chunk, $encryptData, $public_rsa_key);
            $result .= $encryptData;
        }
        $result=base64_encode($result);
                                echo '<pre>';
        print_r($result);
        die;
    }
    
    //私钥解密
    public function PrivateDecrypt()
    {
        $crypto = '';
        //公钥加密字符串
        $encrypted='vHYlf0GzlnZCMct3Aqm6dqbwxrLyFTbFYL9ExXQzGGgEtiEgRcBoneOSxowpttCkeIu+V/eQiGk1PPQ10fWczmdpuPAtN65QA6vu86MYfUplO00SwlX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A==';
        $private_rsa_key = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($private_key, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";
        //调用私钥解密
        $encrypted=base64_decode($encrypted);//反base64
        foreach (str_split($encrypted, 128) as $chunk) {//交易明文超过117字节，所以需要进行分段解密
            openssl_private_decrypt($chunk, $decryptData, $private_rsa_key);//执行解密函数
            $crypto .= $decryptData;//解密后拼接字符串
        }
        echo '<pre>';
        print_r($crypto);
        die;
        return $crypto;
    }

}
